package com.pay.game.central.web.mapper;

import com.github.pagehelper.Page;
import com.pay.game.central.web.bean.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface OrderMapper {

    @Insert("insert into `order`(id, settlement_id, goods_id, price, pay_type, pay_platform, pay_time, sign_time," +
            "   user_id, user_name, goods_name, store_id, store_name)" +
            "values (#{id},#{settlement_id,jdbcType=BIGINT},#{goods_id},#{price,jdbcType=INTEGER}," +
            "#{pay_type, jdbcType=TINYINT},#{pay_platform, jdbcType=TINYINT}," +
            "#{pay_time, jdbcType=TINYINT},#{sign_time, jdbcType=TINYINT}," +
            "#{user_id}, #{user_name}, #{goods_name}, #{store_id}, #{store_name})")
    void insertItem(Order order);

    @Select("select id, goods_id, price, pay_time from `order` where id=#{id}")
    Order findOrderById(@Param("id") String id);

    @Update("update `order` set sign_time=#{sign_time,jdbcType=INTEGER} where id=#{id}")
    void updateSignTime(Order order);

    @Select("<script>" +
            "   select `order`.id,`order`.pay_time,`order`.price,`order`.sign_time,`order`.goods_id,`order`.store_name," +
            "       `order`.store_id,`order`.goods_name,`order`.user_name " +
            "   from `order` " +
            "   where 1=1 " +
            "   <if test=\"rootUserId!=null and rootUserId!=''\"> " +
            "       and user_id like concat(#{rootUserId}, '%')" +
            "   </if>"+
            "   <if test='needSign==0'> " +
            "       and `order`.sign_time!=0 order by `order`.sign_time desc" +
            "   </if>"+
            "   <if test='needSign==1'> " +
            "       and `order`.sign_time=0 order by `order`.pay_time desc" +
            "   </if>"+
            "</script>")
    Page<Map<String, Object>> listSignHistory(@Param("rootUserId") String rootUserId, @Param("needSign") int needSign);

    @Select("<script>" +
            "   select `order`.id,`order`.pay_time,`order`.price,`order`.goods_id,`order`.store_name," +
            "       `order`.store_id,`order`.goods_name,`order`.user_name " +
            "   from `order` " +
            "   where `order`.sign_time=0 " +
            "   <if test=\"rootUserId!=null and rootUserId!=''\"> " +
            "       and user_id like concat(#{rootUserId}, '%')" +
            "   </if>"+
            "   <if test=\"storeId!=null and storeId!=''\"> " +
            "       and store_id like concat(#{storeId}, '%')" +
            "   </if>"+
            "   <if test=\"storeName!=null and storeName!=''\"> " +
            "       and store_name like concat(#{storeName}, '%')" +
            "   </if>"+
            "</script>")
    Page<Map<String, Object>> listNeedSign(
            @Param("rootUserId") String rootUserId,
            @Param("storeId") String storeId,
            @Param("storeName") String storeName);


    @Select("<script>" +
            "   select price,settlement_id from `order` where settlement_id in " +
            "   <foreach collection=\"settlementIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\" >" +
            "       #{item,jdbcType=BIGINT} " +
            "   </foreach>" +
            "</script>")
    List<Order> queryBySettlements(@Param("settlementIds") List<Long> settlementIds);

    @Select("<script>" +
            "   select id,user_name,user_id,store_id,store_name,price," +
            "           goods_name,goods_id,pay_time,pay_type from `order`" +
            "   where pay_time between #{startTime} and #{endTime}" +
            "   <if test=\"id!=null and id!=''\">" +
            "       and id like concat(#{id}, '%')" +
            "   </if>"+
            "   <if test='price!=0'>" +
            "       and price=#{price}" +
            "   </if>"+
            "   <if test=\"storeId!=null and storeId!=''\"> " +
            "       and store_id like concat(#{storeId}, '%')" +
            "   </if>"+
            "   <if test=\"storeName!=null and storeName!=''\"> " +
            "       and store_name like concat(#{storeName}, '%')" +
            "   </if>"+
            "   <if test=\"rootUserId!=null and rootUserId!=''\"> " +
            "       and user_id like concat(#{rootUserId}, '%')" +
            "   </if>" +
            "   order by pay_time desc"+
            "</script>")
    Page<Order> list(@Param("id") String id,
                     @Param("rootUserId") String rootUserId,
                     @Param("price") int price,
                     @Param("startTime") long startTime,
                     @Param("endTime") long endTime,
                     @Param("storeId") String storeId,
                     @Param("storeName") String storeName);


}
